#include "stdafx.h"
#include "PacketCreator.h"
#include "Account.h"
#include "Character.h"

CByteArray CPacketCreator::m_packet = CByteArray();

CByteArray &CPacketCreator::getPacket()
{
	return m_packet;
}

void CPacketCreator::cleanup()
{
	m_packet.destroy();
}

void CPacketCreator::_start(int cmd)
{
	m_packet.clear();

	m_packet.addByte(0x5e);
	m_packet.addInt(0); // taille du packet
	m_packet.addInt(cmd);
}

void CPacketCreator::_finish()
{
	m_packet.setInt(1, m_packet.size() - 5);
}

const CByteArray &CPacketCreator::greeting(int id)
{
	_start(LCMD_GREETING);

	m_packet.addInt(id);

	_finish();

	return m_packet;
}

const CByteArray &CPacketCreator::error(E_ERROR error)
{
	_start(LCMD_ERROR);

	m_packet.addInt(error);

	_finish();

	return m_packet;
}

const CByteArray &CPacketCreator::serverList(CAccount* player, CISCServer::ServerList* list)
{
	_start(LCMD_SERVER_LIST);

	m_packet.addInt(0);
	m_packet.addByte(0);

	m_packet.addString(player->getUsername());

	m_packet.addInt(list->getServerCount());

	m_packet.addInt(-1);
	m_packet.addInt(0);
	m_packet.addString(list->ClusterName);
	m_packet.addString(list->ClusterIP);
	m_packet.addInt(0);
	m_packet.addInt(0);
	m_packet.addInt(1);
	m_packet.addInt(0);

	CISCServer::ServerList::Channel *channel;

	FOR_EACH (list->Channels, i)
	{
		channel = &list->Channels[i];

		m_packet.addInt(0);
		m_packet.addInt(channel->ID);
		m_packet.addString(channel->Name);
		m_packet.addInt(channel->CurrentPlayers >= channel->MaxPlayers ? 1 : 0);
		m_packet.addInt(0);
		m_packet.addInt(channel->CurrentPlayers);
		m_packet.addInt(1);
		m_packet.addInt(channel->MaxPlayers);
	}

	_finish();

	return m_packet;
}

const CByteArray &CPacketCreator::charList(CAccount* player)
{
	_start(ECMD_CHAR_LIST);

	m_packet.addInt(0);

	int charCount = 0;
	for (int i = 0; i < 3; ++i)
	{
		if (player->getCharacter(i))
			charCount++;
	}

	CCharacter* character = null;

	m_packet.addInt(charCount);
	for(int i = 0; i < 3; i++)
	{
		character = player->getCharacter(i);

		if(character)
		{
			m_packet.addInt(i);
			m_packet.addInt(1);
			m_packet.addInt(1);
			m_packet.addInt(0x0b);
			m_packet.addString(character->getName());

			m_packet.addByte(0x0e);
			m_packet.addUByte(0xa6);
			m_packet.addUByte(0xd9);
			m_packet.addByte(0x45);

			m_packet.addUByte(0xa5);
			m_packet.addByte(0x79);
			m_packet.addUByte(0xc8);
			m_packet.addByte(0x42);

			m_packet.addByte(0x3f);
			m_packet.addByte(0x58);
			m_packet.addByte(0x49);
			m_packet.addByte(0x45);

			m_packet.addInt(character->getId());
			m_packet.addInt(0);
			m_packet.addInt(0);
			m_packet.addInt(0);
			m_packet.addInt(0);

			m_packet.addInt(character->getHair());
			m_packet.addInt(character->getHairColor());
			m_packet.addInt(character->getFace());
			m_packet.addByte(character->getSex());
			m_packet.addInt(character->getJob());
			m_packet.addInt(character->getLevel());
			m_packet.addInt(0);
			m_packet.addInt(character->getFOR());
			m_packet.addInt(character->getEND());
			m_packet.addInt(character->getDEX());
			m_packet.addInt(character->getINT());
			m_packet.addInt(0);

			m_packet.addInt(character->getEquipmentSize());
			for(uint j = 0; j < character->getEquipmentSize(); j++)
			{
				m_packet.addInt(character->getEquipment(j));
			}
		}
	}

	m_packet.addInt(charCount);
	for (int i = 0; i < 3; ++i)
	{
		if (player->getCharacter(i))
		{
			m_packet.addInt(0);
			m_packet.addInt(0);
			m_packet.addInt(0);
			m_packet.addByte(0);
		}
	}
	_finish();

	return m_packet;
}

const CByteArray &CPacketCreator::channelIP(const stringc& ip)
{
	_start(ECMD_CHANNEL_IP);

	m_packet.addString(ip);

	_finish();

	return m_packet;
}